package com.ctjsoft.datacollection.controller;

import com.alibaba.fastjson.JSONObject;
import com.ctjsoft.datacollection.core.povo.Result;
import com.ctjsoft.datacollection.entity.KScript;
import com.ctjsoft.datacollection.service.KScriptService;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "首页统计API")
@RestController
@RequestMapping("/dataCollection/monitor")
public class HomeController {

    @Autowired
    KScriptService scriptService;

    /**
     * 首页监控任务统计
     *
     * @return {@link Result}
     */
    @ApiOperation(value = "首页监控任务统计")
    @PostMapping("/taskCount")
    public Result<JSONObject> taskCount() {
        Integer transTaskNum = 0;
        Integer jobTaskNum = 0;
        Integer totalTaskNum = 0;
        List<KScript> list = scriptService.taskCount();
        for (int i = 0; i < list.size(); i++) {
            KScript script = list.get(i);
            if (script.getScriptType() == "1" || script.getScriptType().equals("1")) {
                // 统计运行的转换数量
                transTaskNum = script.getNum();
            } else if (script.getScriptType() == "0" || script.getScriptType().equals("0")) {
                // 统计运行的作业数量
                jobTaskNum = script.getNum();
            }
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("totalTaskNum", transTaskNum + jobTaskNum);
        jsonObject.put("transTaskNum", transTaskNum);
        jsonObject.put("jobTaskNum", jobTaskNum);
        return Result.ok(jsonObject);
    }

    /**
     * 首页监控任务统计
     *
     * @return {@link Result}
     */
    @ApiOperation(value = "首页监控任务统计")
    @PostMapping("/runStatus")
    public Result<List<JSONObject>> runStatus() {
        /*PageHelper page = new PageHelper();
        page.setNumber(1);
        page.setSize(7);
        Pageable pageable = page.getPageable(Sort.by(Sort.Direction.DESC, "startDate"));

        Page<HomeMonitorTaskCounts> pageList = homeMonitorTaskCountsService.findAll(pageable);
        Map<String, HomeMonitorTaskRunRes> resMap = Maps.newHashMap();
        List<HomeMonitorTaskRunRes> list = pageList.get().map(t ->
        {

            HomeMonitorTaskRunRes res = resMap.get(t.getStartDate());
            if (res == null) {
                res = new HomeMonitorTaskRunRes();
            }
            if (t.getType() == 1) {
                res.setTransRunNum(t.getCounts());
            } else {
                res.setJobRunNum(t.getCounts());
            }
            res.setRunTime(t.getStartDate());
            resMap.put(res.getRunTime(), res);
            return res;
        }).collect(Collectors.toList());*/
        return Result.ok();
    }
}
